package com.mr.cai.main.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mr.cai.main.MrCaiApplication;
import com.mr.cai.main.R;
import com.mr.cai.main.activity.base.MrCaiBaseActivity;
import com.mr.cai.main.fragment.NaviFragmentFactory;
import com.mr.cai.main.utils.CommonCoreUtil;

public class MainActivity extends MrCaiBaseActivity implements View.OnClickListener{
    private final static String TAG = MainActivity.class.getName();
    public FragmentManager fragmentManager;
    public NaviFragmentFactory mNaviFragmentFactory;
    private int normalNavTxtColor, selNavTxtColor;
    private View[] navBarViews = new View[4];
    private ImageView[] navIconViews = new ImageView[4];
    private TextView[] navTextViews = new TextView[4];
    public static void launchActivity(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
    }
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        fragmentManager = getSupportFragmentManager();
        mNaviFragmentFactory = new NaviFragmentFactory();
        findView();
    }
    private void findView(){
        initNvaBar();
        showCurrentFragment(NaviFragmentFactory.TITLE_ENUM.HOME_ENUM);
    }
    private void initNvaBar(){
        selNavTxtColor = getResources().getColor(R.color.color_main_green);
        normalNavTxtColor = getResources().getColor(R.color.color_main_gray);
        navBarViews[0] = findViewById(R.id.navi_home);
        navBarViews[1] = findViewById(R.id.navi_group);
        navBarViews[2] = findViewById(R.id.navi_cart);
        navBarViews[3] = findViewById(R.id.navi_my);
        for (int i=0; i<4; i++) {
            navBarViews[i].setOnClickListener(this);
            navIconViews[i] = (ImageView) navBarViews[i].findViewById(R.id.nav_icon);
            navTextViews[i] = (TextView) navBarViews[i].findViewById(R.id.nav_txt);
        }
        navTextViews[0].setText(R.string.navi_home);
        navTextViews[1].setText(R.string.navi_group);
        navTextViews[2].setText(R.string.navi_cart);
        navTextViews[3].setText(R.string.navi_my);
    }
    /**
     * 连续按两次返回键就退出
     */
    private int keyBackClickCount = 0;

    private Handler handler = new Handler();

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            switch (keyBackClickCount++) {
                case 0:
                    CommonCoreUtil.showToast(R.string.app_exit_tip);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            keyBackClickCount = 0;
                        }
                    }, 2000);
                    break;
                case 1:
                    MrCaiApplication.getInstance().exitProcess();
                    break;
            }
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    protected void onDestroy () {
        super.onDestroy();
    }

    @Override
    public void onClick (View view) {
        NaviFragmentFactory.TITLE_ENUM curItem = NaviFragmentFactory.TITLE_ENUM.HOME_ENUM;
        switch (view.getId()){
            case R.id.navi_home:
                curItem = NaviFragmentFactory.TITLE_ENUM.HOME_ENUM;
                break;
            case R.id.navi_group:
                curItem = NaviFragmentFactory.TITLE_ENUM.GROUP_ENUM;
                break;
            case R.id.navi_cart:
                curItem = NaviFragmentFactory.TITLE_ENUM.SHOPPINGCART_ENUM;
                break;
            case R.id.navi_my:
                curItem = NaviFragmentFactory.TITLE_ENUM.USERINFO_ENUM;
                break;
            default:break;
        }
        if (mNaviFragmentFactory.getCurrentItem() != curItem) {
            showCurrentFragment(curItem);
        }
    }
    private NaviFragmentFactory.TITLE_ENUM lastSelectIndex;

    private void checkNavBarProcess(NaviFragmentFactory.TITLE_ENUM indexEnum){
        int index = mNaviFragmentFactory.getTitleEnumNum(indexEnum);
        if(index >= 0 && index <= 3) {
            resetNavBar();
            navTextViews[index].setTextColor(selNavTxtColor);
            switch (index) {
                case 0:
                    navIconViews[0].setBackgroundResource(R.drawable.nav_home_sel);
                    break;
                case 1:
                    navIconViews[1].setBackgroundResource(R.drawable.nav_group_sel);
                    break;
                case 2:
                    navIconViews[2].setBackgroundResource(R.drawable.nav_cart_sel);
                    break;
                case 3:
                    navIconViews[3].setBackgroundResource(R.drawable.nav_my_sel);
                    break;
            }
        }
    }
    private void resetNavBar(){
        for (int i=0; i<4; i++){
            navTextViews[i].setTextColor(normalNavTxtColor);
        }
        navIconViews[0].setBackgroundResource(R.drawable.nav_home_nor);
        navIconViews[1].setBackgroundResource(R.drawable.nav_group_nor);
        navIconViews[2].setBackgroundResource(R.drawable.nav_cart_nor);
        navIconViews[3].setBackgroundResource(R.drawable.nav_my_nor);

    }
    public void showCurrentFragment(NaviFragmentFactory.TITLE_ENUM index) {
        if(index == lastSelectIndex) {
            return;
        }
        lastSelectIndex = index;
        checkNavBarProcess(index);
        if (mNaviFragmentFactory == null) {
            mNaviFragmentFactory = new NaviFragmentFactory();
        }
        if (fragmentManager == null) {
            fragmentManager = getSupportFragmentManager();
        }
        mNaviFragmentFactory.hideAndShowFragment(fragmentManager, index);
    }
}
